/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.unittests.struts.service.beans;

import opiam.admin.faare.PropertiesManager;
import opiam.admin.faare.struts.service.MenusConfig;
import opiam.admin.faare.struts.service.StrutsService;
import opiam.admin.faare.struts.service.beans.JBFeature;
import opiam.admin.faare.struts.service.beans.JBMenus;
import opiam.admin.faare.unittests.TestResult;
import opiam.admin.faare.unittests.TestUtils;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;


/**
 * Test the JBMenus object.
 */
public class JBMenusTest extends TestCase
{
    /** Instance of logger. */
    private static Logger _logger = Logger.getLogger(JBMenusTest.class);

    /** Instance of the trace class. */
    private static TestUtils _test = new TestUtils(_logger);

    /** Menus configuration file. */
    public static final String MENUS_XML_FILE = "/config/service/struts/menus_conf.xml";

    /** Instance of JBMenus. */
    private static JBMenus jbMenusLoaded = new JBMenus();

    /**
     * Creates a new JBMenusTest object.
     *
     * @param arg0  Parameter.
     */
    public JBMenusTest(String arg0)
    {
        super(arg0);

        try
        {
            PropertiesManager.getInstance();
            jbMenusLoaded = MenusConfig.getInstance().readConfig(StrutsService.class.getResourceAsStream(
                        MENUS_XML_FILE));
        }
        catch (Exception e)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("JBMenusTest constructor error");
            msg.append(TestUtils.NEXT);
            msg.append(e.toString());
            _test.displayResultError(msg.toString());
        }
    }

    /**
     * Method of the tests suite.
     *
     * @return Tests result.
     */
    public static Test suite()
    {
        TestSuite suite = new TestSuite();

        suite.addTest(new JBMenusTest("testBegin"));

        suite.addTest(new JBMenusTest("testAddFeature"));
        suite.addTest(new JBMenusTest("testFindFeatureByName"));

        suite.addTest(new JBMenusTest("testEnd"));

        return suite;
    }

    /**
     * Main method of the test class.
     *
     * @param args  Sets of the parameters.
     */
    public static void main(String[] args)
    {
        junit.textui.TestRunner.run(suite());
    }

    /**
     * Method to indicate the beginning of the class test.
     *
     */
    public static void testBegin()
    {
        //  To initialize the tests statement
        TestResult.getInstance().setInitiator("JBMenusTest");

        _test.displayHeaderClass("JBMenusTest");
        assertTrue(true);
    }

    /**
     * Method to indicate the end of the class test.
     *
     */
    public static void testEnd()
    {
        _test.displayFooterClass(null);
        assertTrue(true);

        // To display the tests statement
        _test.displayTestStatement("JBMenusTest");
    }

    /**
     * Test the methods: addFeature and getFeaturesMap.
     *
     */
    public void testAddFeature()
    {
        _test.displayHeaderMethod("testAddFeature");

        try
        {
            JBFeature jbFeature = new JBFeature();

            jbFeature.setName("newFeature");

            Map resultFeaturesMap = new HashMap(jbMenusLoaded.getFeaturesMap());
            Map expectedFeaturesMap = new HashMap(jbMenusLoaded.getFeaturesMap());

            int expectedSize1 = expectedFeaturesMap.size();
            expectedFeaturesMap.put("newFeature", jbFeature);

            int expectedSize2 = expectedFeaturesMap.size();

            int resultSize1 = resultFeaturesMap.size();
            jbMenusLoaded.addFeature(jbFeature);
            resultFeaturesMap = jbMenusLoaded.getFeaturesMap();

            int resultSize2 = resultFeaturesMap.size();

            if (expectedSize2 == resultSize2)
            {
                _test.displayResultSuccess(null);
            }
            else
            {
                _test.displayResultFailure(null);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testAddFeature");
    }

    /**
     * Test the methods: findFeatureByName.
     *
     */
    public void testFindFeatureByName()
    {
        _test.displayHeaderMethod("testFindFeatureByName");

        try
        {
            Map resultFeaturesMap = new HashMap(jbMenusLoaded.getFeaturesMap());
            JBFeature expectedjbFeature = new JBFeature();
            JBFeature resultjbFeature = new JBFeature();

            expectedjbFeature.setName("newFeature");
            jbMenusLoaded.addFeature(expectedjbFeature);

            resultjbFeature = jbMenusLoaded.findFeatureByName("newFeature");

            if (expectedjbFeature.getName().equals(resultjbFeature.getName()))
            {
                _test.displayResultSuccess(null);
            }
            else
            {
                _test.displayResultFailure(null);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testFindFeatureByName");
    }
}
